package com.artron.mediaartron.data.task;

import com.artron.baselib.entity.Response;
import com.artron.mediaartron.data.entity.Invoice;
import com.artron.mediaartron.data.entity.OrderData;
import com.artron.mediaartron.data.entity.Works;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.OrderApi;
import com.google.gson.Gson;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
import retrofit2.http.Query;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-27.
 */

public class OrderTask {

    public static Observable<Response<OrderData>> createRapidOrder(String couponCode, String addressId, float expressFee,
                                                                   Invoice invoice, Works works) {

        JsonBean jsonBean = new JsonBean().setCouponCode(couponCode)
                .setAddressId(addressId)
                .setExpressFee(expressFee)
                .setInvoice(invoice)
                .setWorks(works);
        Gson gson = RetrofitHelper.buildJsonParser();
        final String jsonStr = gson.toJson(jsonBean);

        RequestBody body = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse("application/json");
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                BufferedInputStream bufIs = new BufferedInputStream(new ByteArrayInputStream(jsonStr.getBytes()));
                byte[] buf = new byte[1024 * 100];
                int len;
                while ((len = bufIs.read(buf)) != -1) {
                    sink.write(buf, 0, len);
                    sink.flush();
                }
                bufIs.close();
            }
        };


        return RetrofitHelper.getOrderApi().createRapidOrder(body);
    }

    private static class JsonBean{
        private String couponCode;
        private String addressId;
        private float expressFee;
        private Invoice invoice;
        private Works works;

        public String getCouponCode() {
            return couponCode;
        }

        public JsonBean setCouponCode(String couponCode) {
            this.couponCode = couponCode;
            return this;
        }

        public String getAddressId() {
            return addressId;
        }

        public JsonBean setAddressId(String addressId) {
            this.addressId = addressId;
            return this;
        }

        public float getExpressFee() {
            return expressFee;
        }

        public JsonBean setExpressFee(float expressFee) {
            this.expressFee = expressFee;
            return this;
        }

        public Invoice getInvoice() {
            return invoice;
        }

        public JsonBean setInvoice(Invoice invoice) {
            if (invoice == null) {
                invoice = new Invoice();
            }
            this.invoice = invoice;
            return this;
        }

        public Works getWorks() {
            return works;
        }

        public JsonBean setWorks(Works works) {
            this.works = works;
            return this;
        }
    }
}
